package com.lightkits.wms;

import com.lightkits.common.db.dao.CommonMapperConfig;
import com.lightkits.infra.mapper.HibernateValidatorMapperConfig;
import com.lightkits.wms.database.dao.MapperConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

import javax.servlet.ServletContextListener;

/**
 * @author wei.zhan@hand-china.com
 */
@MapperScan(basePackageClasses = {MapperConfig.class, CommonMapperConfig.class, HibernateValidatorMapperConfig.class})
@SpringBootApplication(scanBasePackages = "com.lightkits", exclude = {SecurityAutoConfiguration.class})
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.lightkits")
public class LightkitsApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(LightkitsApplication.class, args);
    }

    @Autowired
    @Qualifier("LightKitsWebListener")
    private ServletContextListener lightKitsWebListener;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(LightkitsApplication.class);
    }

    @Bean
    public ServletListenerRegistrationBean<ServletContextListener> lightkitsServletContextListener() {
        return new ServletListenerRegistrationBean<>(lightKitsWebListener);
    }
}
